package com.mail.transfer;

import com.mail.transfer.email.PopInfo;
import com.mail.transfer.email.schedule.DynamicTimedTask;
import com.mail.transfer.email.schedule.MailPullScheduledTask;
import com.mail.transfer.email.server.MailNettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetSocketAddress;

/**
 * @author xiaocg
 */
@SpringBootApplication
@EnableScheduling
public class TransferApplication {


	public static void main(String[] args) {

		String method = args[0].split("=")[1];
		String listenTcp = args[1].split("=")[1];

		ConfigurableApplicationContext run = SpringApplication.run(TransferApplication.class, args);
		PopInfo popInfo = run.getBean(PopInfo.class);
		JavaMailSender javaMailSender = run.getBean(JavaMailSender.class);

		if ("send".equals(method)){
			//接收邮件发 开启socket 监听
			MailPullScheduledTask task = new MailPullScheduledTask(popInfo,listenTcp);
			DynamicTimedTask dynamicTimedTask = new DynamicTimedTask(task);
			dynamicTimedTask.startCron();
		}else if ("recv".equals(method)){
			//拉取邮件发 建立socket 连接
			String[] address = listenTcp.split(":");
			InetSocketAddress inetSocketAddress = new InetSocketAddress(address[0], Integer.parseInt(address[1]));
			MailNettyServer server = new MailNettyServer();
			server.start(inetSocketAddress,javaMailSender);
		}
	}

}
